package com.charmzenus.champion.presenter.fragment.main;

import android.os.Handler;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.charmzenus.champion.adapter.ItemClickListener;
import com.charmzenus.champion.adapter.main.MainOrderAdapter;
import com.charmzenus.champion.base.BasePresenter;
import com.charmzenus.champion.ui.fragment.main.MainOrder;
import com.charmzenus.champion.utils.LogUtil;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;


/**
 * 首页订单的Presenter
 */
public class MainOrderFragmentPresenter extends BasePresenter implements ItemClickListener {

    private MainOrder mainOrder;
    private List<String> listData = null;
    private MainOrderAdapter adapter;


    public MainOrderFragmentPresenter(MainOrder mainOrder) {
        this.mainOrder = mainOrder;
        listData = new ArrayList<>();
    }

    /**
     * 初始化下拉刷新框架
     */
    public void initRefreshLayout() {
        RefreshLayout refreshLayout = mainOrder.getRefreshLayout();
        if (refreshLayout == null) return;


        refreshLayout.autoRefresh();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                int time = 3000;
                refreshlayout.finishRefresh(time/*,false*/);//传入false表示刷新失败
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        onRefreshList();
                    }
                }, time);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {

                int time = 3000;
                refreshlayout.finishLoadMore(time/*,false*/);//传入false表示加载失败
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        onLoadMoreList();
                    }
                }, time);
            }
        });


    }


    /**
     * 设置列表数据
     */
    private void setRecyclerViewListData() {

        RecyclerView recyclerView = mainOrder.getRecyclerView();
        if (recyclerView == null) return;


        if (adapter == null) {
            adapter = new MainOrderAdapter(mainOrder.getContext(), listData);
            recyclerView.setAdapter(adapter);
            //为RecyclerView设置内容的布局（这里是一个线性布局）
            LinearLayoutManager manager = new LinearLayoutManager(mainOrder.getContext());
            recyclerView.setLayoutManager(manager);

            recyclerView.addItemDecoration(new DividerItemDecoration(mainOrder.getContext(), DividerItemDecoration.VERTICAL));
            adapter.setListener(this);
        } else {
            adapter.setListData(listData);
        }
    }


    @Override
    public void onItemClick(View view, int position) {

        LogUtil.L("AAA", "position = " + position);

    }


    private void onRefreshList() {
        listData.clear();
        for (int i = 0; i < 10; i++) {
            String add = String.valueOf(i + 1);
            listData.add(add);
        }
        setRecyclerViewListData();
    }

    private void onLoadMoreList() {

        String max = listData.get(listData.size()-1);
        int maxInt = Integer.parseInt(max);
        for (int i = 0; i < 10; i++) {
            String add = String.valueOf(maxInt + i + 1);
            listData.add(add);
        }
        setRecyclerViewListData();
    }
}
